﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace PRO.ORM
{
    [AttributeUsage(AttributeTargets.All, Inherited = false)]
    public class EntityAttribute : Attribute
    {
        public EntityAttribute() {
            
        }
        /// <summary>
        /// 字段数据类型
        /// </summary>
        public SqlDbType SqlDbType { get; set; }
        /// <summary>
        /// 字段长度
        /// </summary>
        public int Len { get; set; }
        private bool _PrimaryKey = false;
        /// <summary>
        /// 是否是主键列
        /// </summary>
        public bool PrimaryKey
        {
            get { return _PrimaryKey; }
            set { _PrimaryKey = value; }
        }
        public object DefaultValue { get; set; }
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName { get; set; }

        private bool _NotNull = true;
        /// <summary>
        /// 是否可以为空,默认不准为空(True)
        /// </summary>
        public bool NotNull
        {
            get { return _NotNull; }
            set { _NotNull = value; }
        }
    }
    public class EntityAttributeInfo
    {

        /// <summary>
        /// 数据库类型
        /// </summary>
        public SqlDbType SqlDbType { get; set; }
        /// <summary>
        /// 数据库类型长度
        /// </summary>
        public int Len { get; set; }
       
        private string _Feild;
        /// <summary>
        /// 数据字段名称
        /// </summary>
        public string Feild
        {
            get { return _Feild.ToLower(); }
            set { _Feild = value; }
        }
        /// <summary>
        /// 字段的值
        /// </summary>
        public object Value { get; set; }
        private bool _PrimaryKey = false;
        /// <summary>
        /// 是否是主键列
        /// </summary>
        public bool PrimaryKey
        {
            get { return _PrimaryKey; }
            set { _PrimaryKey = value; }
        }
        /// <summary>
        /// 默认值
        /// </summary>
        public object DefaultValue { get; set; }
        /// <summary>
        /// 是否准许为空，默认为不准为空
        /// </summary>
        public bool NotNull { get; set; }
    }
    /// <summary>
    /// 数据查询条件实体类
    /// </summary>
    public class WhereInfo
    {
        private string _Feild;
        /// <summary>
        /// 字段名
        /// </summary>
        public string Feild
        {
            get { return _Feild; }
            set { _Feild = value; }
        }
        private string _ParamName;
        /// <summary>
        /// Sql变量名
        /// </summary>
        public string ParamName
        {
            get { return _ParamName; }
            set { _ParamName = value; }
        }
        private object _Value;
        /// <summary>
        /// 字段值
        /// </summary>
        public object Value
        {
            get { return _Value; }
            set { _Value = value; }
        }
        private string _Operator;
        /// <summary>
        /// 操作符（=，<>,>,>=,<,<=）
        /// </summary>
        public string Operator
        {
            get { return _Operator; }
            set { _Operator = value; }
        }
        private SqlDbType _SqlDbType = SqlDbType.NVarChar;
        /// <summary>
        /// 数据类型
        /// </summary>
        public SqlDbType SqlDbType
        {
            get { return _SqlDbType; }
            set { _SqlDbType = value; }
        }
        private int _Len;
        /// <summary>
        /// 数据长度
        /// </summary>
        public int Len
        {
            get { return _Len; }
            set { _Len = value; }
        }
    }
    /// <summary>
    /// 实体类属性操作类
    /// </summary>
    public class EntityAttributeLib
    {
        /// <summary>
        /// 遍历实体类，返回实体类的列名、值、对应数据库类型、对应数据库类型长度
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public static List<EntityAttributeInfo> GetProperties<T>(T entity, List<EntityAttributeInfo> feilds,string[] NoexeFeilds) where T : BaseEntity
        {
            if (entity == null)
            {
                return null;
            }
            System.Reflection.PropertyInfo[] properties = entity.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

            if (properties.Length <= 0)
            {
                return null;
            }
            foreach (System.Reflection.PropertyInfo item in properties)
            {
                if (!ArrayIsExist(NoexeFeilds, item.Name))
                {
                    if (feilds.FindAll(delegate(EntityAttributeInfo info) { return info.Feild.ToLower() == item.Name.ToLower(); }).Count > 0)
                        throw new Exception("已存在字段名：" + item.Name + "！");
                    else
                    {
                        EntityAttributeInfo info = new EntityAttributeInfo();
                        EntityAttribute attr = (EntityAttribute)Attribute.GetCustomAttribute(item, typeof(EntityAttribute));
                        if (attr == null)
                            throw new Exception("必须给字段[" + item.Name + "]设置属性！");
                        else
                        {
                            info.SqlDbType = attr.SqlDbType;
                            info.Len = attr.Len;
                            info.PrimaryKey = attr.PrimaryKey;
                            info.Value = item.GetValue(entity, null);
                            info.DefaultValue = attr.DefaultValue;
                            info.NotNull = attr.NotNull;
                            info.Feild = item.Name.ToLower();
                            feilds.Add(info);
                        }
                    }
                }
                //if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                //{
                //    tStr += string.Format("{0}:{1}:{2},", name, value, des);
                //}
                //else
                //{
                //    getProperties(value);
                //}
            }
            return feilds;
        }
        /// <summary>
        /// 判断数组里是否存在某值
        /// </summary>
        /// <param name="arry"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ArrayIsExist(string[] arry, string str)
        {
            bool r = false;
            if (arry != null)
            {
                foreach (string item in arry)
                {
                    if (item.ToLower().Equals(str.ToLower()))
                    {
                        r = true;
                        break;
                    }
                }
            }
            return r;
        }
        /// <summary>
        /// 获取实体类里面配置的表名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static string GetTableName<T>(T entity) where T : BaseEntity
        {
            Type tp = typeof(T);
            object[] obj = tp.GetCustomAttributes(typeof(EntityAttribute), true);
            if (obj.Length > 0)
            {
                EntityAttribute attr = obj[0] as EntityAttribute;
                if (attr != null)
                {
                    return attr.TableName;
                }
            }
            return null;
        }
    }
 
}
